#include <iostream>
#include <cstdio>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

const std::string myenv="HELLO=AAAAAAAAAAAAAAAAAAAAAA";

extern char **environ;
int main()
{
    putenv((char*)myenv.c_str());
    pid_t id = fork();
    if(id == 0)
    {
        char *const argv[] = {
            (char*)"code",
            nullptr 
        };
        
        char *const env[] = {
            (char*)"HELLO=xlf1",
            (char*)"HELLO=xlf2",
            (char*)"HELLO=xlf3",
            (char*)"HELLO=xlf4"
        };
        execvpe("./code",argv,environ);

        //execl("./code","code",nullptr);

        //execvpe("./code",argv, env);
        
        //execl("./code","code",nullptr);

       // char *argv[] = {
       //    (char*)"ls",
       //    (char*)"--color",
       //    (char*)"-a",
       //    (char*)"-l",
       //     nullptr
       // };

       // //execvp("ls",argv);
       // execvp(argv[0],argv);
        
        //char *argv[] = {
        //   (char*)"ls",
        //   (char*)"--color",
        //   (char*)"-a",
        //   (char*)"-l",
        //    nullptr
        //};
        //execv("/usr/bin/ls",argv);
        
       // execlp("ls","ls --color -l -a",nullptr);

        exit(1); // 替换失败返回1
    }
    // father
    pid_t rid = waitpid(id, nullptr, 0);//阻塞式等待子进程
    if(rid > 0)
    {
        printf("等待子进程成功！\n");
    }

    return 0;
}


//int main()
//{
//    printf("我是myexec,pid: %d\n", getpid());
//    int n = execl("/bin/lsss","sss", "-l", "--color","-a", nullptr);
//    printf("execl return val: %d\n", n);
//    
//    //execl("./other", "other", nullptr);
//    
//    //execl("/bin/ls","ls", "-l", "-a", nullptr);
//    //execl("/usr/bin/top", "top", nullptr);
//    return 0;
//}
